package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsServiceNameConstant;
import cc.rengu.igas.bsps.common.constant.BspsTreeNodeConstant;
import cc.rengu.igas.bsps.common.dao.BspSequenceMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntApplyMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspSequenceMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntApplyMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntApplyInfo;
import cc.rengu.igas.bsps.common.enums.MchntApplyStatusEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntApplyRequest;
import cc.rengu.igas.bsps.facade.response.MchntApplyResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SmsService;
import cc.rengu.oltp.service.realize.impl.SmsServiceImpl;
import cc.rengu.oltp.utility.util.*;

/**
 * 商户入驻申请业务
 *
 * @author zhangxuran (联调修改)
 * @version 1.0.0
 * @date 2020.03.12
 */
public class MchntApplyService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntApplyRequest mchntApplyRequest = new MchntApplyRequest();
        ConvertUtil.convertOutput(mchntApplyRequest);
        return mchntApplyRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        MchntApplyRequest mchntApplyRequest = (MchntApplyRequest) request;
        /*获取请求信息*/
        String instId = mchntApplyRequest.getHeader().getInstId();
        String channelId = mchntApplyRequest.getHeader().getChanlId();
        String mchntName = mchntApplyRequest.getMchntName();
        String managementScope = mchntApplyRequest.getManagementScope();
        String area = mchntApplyRequest.getArea();
        String address = mchntApplyRequest.getAddress();
        String contactName = mchntApplyRequest.getContactName();
        String contactPhone = mchntApplyRequest.getContactPhone();
        //APP渠道，openId默认先为空
        String openId = "APP".equals(channelId)?"OPENID":mchntApplyRequest.getOpenId();
        String smsAuthId = mchntApplyRequest.getSmsAuthId();
        String smsCode = mchntApplyRequest.getSmsCode();
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(mchntName) || StringUtil.isEmptyOrNull(managementScope)
                || StringUtil.isEmptyOrNull(area) || StringUtil.isEmptyOrNull(address) || StringUtil.isEmptyOrNull(contactName)
                || StringUtil.isEmptyOrNull(contactPhone) || StringUtil.isEmptyOrNull(openId)
                || StringUtil.isEmptyOrNull(smsAuthId) || StringUtil.isEmptyOrNull(smsCode)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        //校验短信验证码
        SmsService smsService = new SmsServiceImpl();
        smsService.verifySmsCode(instId, "06", smsCode, smsAuthId);
        String province = area.substring(0, 2) + "0000";
        String city = area.substring(0, 4) + "00";
        BspsMchntApplyMapper mchntApplyMapper = new BspsMchntApplyMapperImpl();
        String dateFormat = DateUtil.getCurrentDateTime("yyyyMMddhhmmss");
        BspSequenceMapper bspSequenceMapper = new BspSequenceMapperImpl();
        String applyId = bspSequenceMapper.getNextMchntApplyId(8);
        if (null == applyId) {
            rglog.error("生成单号流水序号失败");
            throw new BizException(OltpRpcdEnum.GENERATE_SEQUENCE_ERROR);
        }
        // 插入数据库的数据
        MchntApplyInfo mchntApplyInfo = new MchntApplyInfo();
        String id = RandomUtil.getUUID();
        mchntApplyInfo.setId(id);
        mchntApplyInfo.setApplyId(dateFormat + applyId);
        mchntApplyInfo.setInstId(instId);
        mchntApplyInfo.setMchntName(mchntApplyRequest.getMchntName());
        mchntApplyInfo.setManagement(mchntApplyRequest.getManagementScope());
        mchntApplyInfo.setProvince(province);
        mchntApplyInfo.setCity(city);
        mchntApplyInfo.setArea(mchntApplyRequest.getArea());
        mchntApplyInfo.setStreet(mchntApplyRequest.getStreet());
        mchntApplyInfo.setAddress(mchntApplyRequest.getAddress());
        mchntApplyInfo.setContactName(mchntApplyRequest.getContactName());
        mchntApplyInfo.setContactPhone(mchntApplyRequest.getContactPhone());
        mchntApplyInfo.setOpenId(openId);
        mchntApplyInfo.setApplyStatus(MchntApplyStatusEnum.APPLYING.getApplyStatus());
        mchntApplyInfo.setApplyTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = mchntApplyMapper.insertBspsMchntApplyInfo(mchntApplyInfo);
        if (0 != iReturnCode) {
            rglog.error("商户入驻申请<{}>信息表失败,iReturnCode:<{}>", mchntApplyRequest.getMchntName(), iReturnCode);
            throw new BizException(RespCodeEnum.REGISTER_MCHNT_APPLY_INFO_ERROR.getRespCode(), RespCodeEnum.REGISTER_MCHNT_APPLY_INFO_ERROR.getRespDesc());
        }
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.MCHNT_APPLY_INFO, mchntApplyInfo);
        iReturnCode = asynnotify.transactionNotice(BspsServiceNameConstant.MCHNT_APPLY_PUSH_TO_MANAGER_SERVICE, 1, 0, 0);
        if (0 != iReturnCode) {
            rglog.error("异步调用消息推送服务失败,iReturnCode:<{}>", iReturnCode);
        }
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntApplyResponse mchntApplyInfoResponse = (MchntApplyResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntApplyInfoResponse);
    }
}
